﻿using SiliconSelect.Base;
using SiliconSelect.BLL;
using SiliconSelect.DAL.DataEntity;
using SiliconSelect.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace SiliconSelect.Views.UserOperatorDialog
{
    /// <summary>
    /// ChangeUserWindow.xaml 的交互逻辑
    /// </summary>
    public partial class ChangeUserWindow : Window
    {
        public ChangeUserWindow()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 切换用户的逻辑
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void BtnChangeUerClick(object sender, RoutedEventArgs e)
        {
            Window window = GetWindow(sender as DependencyObject);
            UserModel? userInfo = window.DataContext as UserModel;
            if (userInfo != null)
            {
                if (!string.IsNullOrEmpty(userInfo.Username) && !string.IsNullOrEmpty(userInfo.Password))
                {
                    DataResult<UserEntity> result = LocalDataAccess.CheckUserInfo(userInfo.Username, userInfo.Password);
                    if (result.State)
                    {
                        UserEntity user = result.Data!;
                        GlobalValues.UserInfo = user;
                        MessageBox.Show("切换成功!");
                        window.Close();
                    }
                    else
                    {
                        MessageBox.Show($"切换失败:{result.Message}");
                    }
                }
                else
                {
                    MessageBox.Show("用户名或者密码不能为空!");
                }
            }
        }



        private void BtnCancelClick(object sender, RoutedEventArgs e)
        {
            Window window = GetWindow(sender as DependencyObject);
            window.Close();
        }
    }
}
